package com.caiden_micheal.user.mapper;

import com.caiden_micheal.permission.domain.Permission;
import com.caiden_micheal.user.domain.User;
import com.caiden_micheal.user.domain.UserAuthority;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserAuthorityMapper {
    List<UserAuthority> findBatchAuthorityIdByUserId(Long id);

    @Select("SELECT * FROM sys_user_authority WHERE authority_id=#{permissionId}")
    List<UserAuthority> findByAuthorityId(@Param("permissionId") Long permissionId);

    void addUserAuthority(@Param("userAuthority") UserAuthority userAuthority);

    void batchAddUserAuthority(@Param("userList") List<User> userList, @Param("permission") Permission permission);

    @Delete("DELETE FROM sys_user_authority WHERE user_id=#{userId} AND authority_id=#{permissionId}")
    void removeByUserIdAndPermissionId(@Param("userId") Long userId, @Param("permissionId") Long permissionId);

    @Select("SELECT authority_id FROM sys_user_authority WHERE user_id=#{userId}")
    List<Long> findAuthorityIdsByUserId(@Param("userId") Long userId);

    @Select("SELECT * FROM sys_user_authority WHERE authority_id=#{permissionId} AND user_id=#{userId}")
    UserAuthority findByAuthorityIdAndUserId(@Param("permissionId") Long permissionId, @Param("userId") Long userId);

    void batchAddByAuthorityListAndUser(@Param("permissionList") List<Permission> permissionList, @Param("user") User user);

    @Select("SELECT user_id FROM sys_user_authority WHERE authority_id=#{permissionId}")
    List<Long> findUserIdsByAuthorityId(@Param("permissionId") Long id);

    @Delete("DELETE FROM sys_user_authority WHERE authority_id=#{permissionId}")
    void removeByPermissionId(@Param("permissionId") Long permissionId);
}
